﻿using System;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class campaignStatsOutput : Api_BaseOutput
   {
      public campaignStatsResults result { get; set; }
      public campaignStatsParms inputParms { get; private set; }
      // --------------------------------------------------------
      public campaignStatsOutput( campaignStatsInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new campaignStatsResults();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "cid: {0}, emails_sent: {1}, opens: {2} ",
            this.inputParms.cid, this.result.emails_sent, this.result.opens );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_campaignStats rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // ------------------------------------ get values
         try
         {
            int i; DateTime dt;
            campaignStatsResults stats = new campaignStatsResults();
            foreach ( XElement entry in xml.Elements() )
            {
               if ( entry.Name == "syntax_errors" )
               { int.TryParse( entry.Value, out i ); stats.syntax_errors = i; }
               else if ( entry.Name == "hard_bounces" )
               { int.TryParse( entry.Value, out i ); stats.hard_bounces = i; }
               else if ( entry.Name == "soft_bounces" )
               { int.TryParse( entry.Value, out i ); stats.soft_bounces = i; }
               else if ( entry.Name == "unsubscribes" )
               { int.TryParse( entry.Value, out i ); stats.unsubscribes = i; }
               else if ( entry.Name == "abuse_reports" )
               { int.TryParse( entry.Value, out i ); stats.abuse_reports = i; }
               else if ( entry.Name == "forwards" )
               { int.TryParse( entry.Value, out i ); stats.forwards = i; }
               else if ( entry.Name == "forwards_opens" )
               { int.TryParse( entry.Value, out i ); stats.forwards_opens = i; }
               else if ( entry.Name == "opens" )
               { int.TryParse( entry.Value, out i ); stats.opens = i; }
               else if ( entry.Name == "last_open" )
               { DateTime.TryParse( entry.Value, out dt ); stats.last_open = dt; }
               else if ( entry.Name == "unique_opens" )
               { int.TryParse( entry.Value, out i ); stats.unique_opens = i; }
               else if ( entry.Name == "clicks" )
               { int.TryParse( entry.Value, out i ); stats.clicks = i; }
               else if ( entry.Name == "unique_clicks" )
               { int.TryParse( entry.Value, out i ); stats.unique_clicks = i; }
               else if ( entry.Name == "users_who_clicked" )
               { int.TryParse( entry.Value, out i ); stats.users_who_clicked = i; }
               else if ( entry.Name == "last_click" )
               { DateTime.TryParse( entry.Value, out dt ); stats.last_click = dt; }
               else if ( entry.Name == "emails_sent" )
               { int.TryParse( entry.Value, out i ); stats.emails_sent = i; }
            }
            this.result = stats;
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_campaignStats jsonResult = jss.Deserialize<x_campaignStats>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // -----------------------------
      private campaignStatsResults FormatResults( x_campaignStats x_results )
      {
         DateTime dTemp;
         campaignStatsResults results = new campaignStatsResults();
         //
         results.syntax_errors = (int)x_results.syntax_errors;
         results.hard_bounces = (int)x_results.hard_bounces;
         results.soft_bounces = (int)x_results.soft_bounces;
         results.unsubscribes = (int)x_results.unsubscribes;
         results.abuse_reports = (int)x_results.abuse_reports;
         results.forwards = (int)x_results.forwards;
         results.forwards_opens = (int)x_results.forwards_opens;
         results.opens = (int)x_results.opens;
         DateTime.TryParse( x_results.last_open, out dTemp ); results.last_open = dTemp;
         results.unique_opens = x_results.unique_opens;
         results.clicks = x_results.clicks;
         results.unique_clicks = x_results.unique_clicks;
         DateTime.TryParse( x_results.last_click, out dTemp ); results.last_click = dTemp;
         results.users_who_clicked = (int)x_results.users_who_clicked;
         results.emails_sent = (int)x_results.emails_sent;
         //
         return results;
      }

      #endregion
   }
}
